﻿using System.Collections.Generic;
using Cms.Application.Admin.Views;
using Cms.Data;
using Cms.Domain;
using Cms.Domain.RepositoryInterfaces;

namespace Cms.Application.Admin.Presenters
{
    public class PublicAdminMenuPresenter : BasePresenter
    {
        private readonly Administrator _administrator;
        private readonly IPublicAdminMenuView _view;

        public PublicAdminMenuPresenter(IPublicAdminMenuView view)
            : this(view, new RepositoryFactory())
        {
        }
        
        public PublicAdminMenuPresenter(IPublicAdminMenuView view, IRepositoryFactory repositoryFactory)
            : base(view, repositoryFactory)
        {
            _view = view;
            _administrator = AdministratorRepository.GetAdministrator(_view.AdministratorID);
        }

        public void Initialize()
        {
            IList<VisitedAdminUrl> urls = new List<VisitedAdminUrl>();
            int limit = _administrator.VisitedAdminUrls.Count - 20;
            if (limit < 0)
                limit = 0;
            for (int i = _administrator.VisitedAdminUrls.Count - 1; i >= limit; i--)
            {
                urls.Add(_administrator.VisitedAdminUrls[i]);
            }
            _view.SetLatestVisitedAdminUrls(urls);
            _view.SetEditLinks(_view.CurrentPage);
            if (!_view.CurrentPage.AllowsAdministrator(_administrator))
            {
                _view.HideEditLink();
            }
        }
    }
}